<?php
/**
 * Created by PhpStorm.
 * User: stupboy
 * Date: 2018/2/4
 * Time: 0:24
 */

namespace app\base\service;

use think\Config;

class JPush
{
    private $appKey       = "7787fd27355ebf708554e7dc";// 7787fd27355ebf708554e7dc daaafb369d2efc08efc6a239
    private $masterSecret = "0b7dc5f2f0d3bafbd96b1ed7";// 0b7dc5f2f0d3bafbd96b1ed7 65a09a72b53f8c15a4ef7b66
    private $host         = "https://api.jpush.cn/v3/push";

    public function setKey($appKey, $masterSecret) {
        $this->appKey       = $appKey;
        $this->masterSecret = $masterSecret;
    }

    public function pushMessage($registration_id,$messages,$notice='',$m_time=86400){
        $base64 = base64_encode("$this->appKey:$this->masterSecret");
        $header = ["Authorization:Basic $base64","Content-Type:application/json"];
        $data   = [];
        $data['platform'] = ["android","ios"];          //目标用户终端手机的平台类型android,ios,winphone
        $data['audience'] = [
            "registration_id"=>$registration_id
        ];
        //通知
        if(!empty($notice)){
            $alert  = is_array($notice) ? $notice['alert']  : $notice;
            $action = is_array($notice) ? $notice['action'] : 'action';
            $data['notification'] = [
                'android'=>[
                    'alert'=>$alert,
                    'extras'=>[
                        'action'=>$action
                    ]
                ],
                'ios'=>[
                    'alert'=>$alert,
                    'sound'=>'sound.caf',
                    'badge'=>1,
                    'extras'=>[
                        'action'=>$action
                    ]
                ]
            ];
        }
        //自定义信息
        $data['message'] = [
            "msg_content"=>$messages,
            "extras"=>json_decode($messages)
        ];
        //附加选项
        $data['options'] = [
            "sendno"=>time(),
            "time_to_live"=>$m_time,      //保存离线时间的秒数默认为一天
            "apns_production"=>1,        //指定 APNS 通知发送环境：0开发环境，1生产环境。
        ];
        $param = json_encode($data);
        $res   = $this->push_curl($param,$header);
        if($res){       //得到返回值--成功已否后面判断
            return $res;
        }else{          //未得到返回值--返回失败
            return false;
        }
    }

    public function pushNotice($registration_id,$messages,$m_time=86400){
        $base64 = base64_encode("$this->appKey:$this->masterSecret");
        $header = ["Authorization:Basic $base64","Content-Type:application/json"];
        $data   = [];
        $data['platform'] = ["android","ios"];          //目标用户终端手机的平台类型android,ios,winphone
        $data['audience'] = [
            "registration_id"=>$registration_id
        ];
        $data['notification'] = [
            'android'=>[
                'alert'=>$messages
            ],
            'ios'=>[
                'alert'=>$messages,
                'sound'=>'sound.caf',
                'badge'=>1,
                'extras'=>[
                    'news_id'=>1,
                    'my_key'=>2
                ]
            ]
        ];
        //附加选项
        $data['options'] = [
            "sendno"=>time(),
            "time_to_live"=>$m_time,      //保存离线时间的秒数默认为一天
            "apns_production"=>1,        //指定 APNS 通知发送环境：0开发环境，1生产环境。
        ];
        $param = json_encode($data);
        $res   = $this->push_curl($param,$header);
        if($res){       //得到返回值--成功已否后面判断
            return $res;
        }else{          //未得到返回值--返回失败
            return false;
        }
    }

    public function pushToUser($user_id, $device_token, $device_type, $text, $messages, $m_time='86400'){
        $base64 = base64_encode("$this->appKey:$this->masterSecret");
        $header = ["Authorization:Basic $base64","Content-Type:application/json"];
        $text   = $text ? $text : '';
        $data   = [];
        $data['platform'] = ["android","ios"];          //目标用户终端手机的平台类型android,ios,winphone
        $data['audience'] = [
            "registration_id"=>[$device_token]
        ];
        //自定义信息
        $data['message'] = [
            "msg_content"=>$messages,
            "extras"=>json_decode($messages)
        ];
        //附加选项
        $data['options'] = [
            "sendno"=>time(),
            "time_to_live"=>$m_time,      //保存离线时间的秒数默认为一天
            "apns_production"=>1,        //指定 APNS 通知发送环境：0开发环境，1生产环境。
        ];
        $param = json_encode($data);
        $res   = $this->push_curl($param,$header);
        if($res){       //得到返回值--成功已否后面判断
            return $res;
        }else{          //未得到返回值--返回失败
            return false;
        }
    }

    public function push_curl($param="",$header="") {
        if (empty($param)){
            return false;
        }
        $postUrl = $this->host;
        $curlPost = $param;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$postUrl);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
}